<?php

/*
 * Author: Quan Van Sinh
 * Email : qvsinh@gmail.com
 * Mobile: 0972405165
 */

class outsite {

    function outsite() {
        
    }

    function get_domain_name_from_text($text) {
        // get host name from URL
        preg_match("/^(http:\/\/)?([^\/]+)/i", $text, $matches);
        $host = $matches[2];
        preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
        return $matches[0];
    }

    function get_domain_from_url($url) {
        preg_match("/^(http:\/\/)?([^\/]+)/i", $url, $matches);
        return $host = $matches[2];
    }

    function getYoutubeIdFromUrl($url) {
        if (preg_match('![?&]{1}v=([^&]+)!', $url . '&', $m)) {
            return $m[1];
        } else {
            return false;
        }
    }

    function getYoutubeIdFromEmbed($embed) {
        preg_match('#(?<=youtube\.com/v/)\w+#', $embed, $matches);
        print_r($matches);
    }

    function extractDomainData($url) {
        global $db;
        $result = false;
        $domain = $this->get_domain_from_url($url);

        $content = $this->get_content($url);

        $dom = new DOMDocument('1.0', 'UTF-8');

        $dom->loadHTML($content);

        $title = $dom->getElementsByTagName("title")->item(0)->nodeValue;

        $metas = $dom->getElementsByTagName('meta');
        foreach ($metas as $meta) {
            if (strtolower($meta->getAttribute('name')) == 'description') {
                $description = $meta->getAttribute('content');
            }
        }

        $arrImg = $this->get_img_thumb($domain, $dom);

        if (mb_detect_encoding($title, 'ISO-8859-1,UTF-8') != "UTF-8") {
            $title = utf8_decode($title);
            $description = utf8_decode($description);
        }
        $result = array(
            'title' => $title,
            'metaTags' => $description,
            'images' => $arrImg
        );


        return $result;
    }

    function get_img_thumb($website, $dom) {
        $arrImg = array();
        $arrobject = array(
            'vnexpress.net' => "cl_content",
            'dantri.com.vn' => "id_ctl00_IDContent_ctl00_divContent",
            'kenh14.vn' => "cl_postpadding",
            'ngoisao.net' => "id_CContainer",
            'zing.vn' => "id_content_document",
            '24h.com.vn' => "cl_baiviet-container",
            'hn.24h.com.vn' => "cl_baiviet-container",
            'hcm.24h.com.vn' => "cl_baiviet-container",
            'tuoitre.vn' => "id_divContent",
            'nld.com.vn' => "cl_detail",
            'tienphong.vn' => "cl_contentNewsDetail",
            'nhandan.org.vn' => "cl_element article",
            'cpv.org.vn' => "id_ctl00_main_content_Uctrl_NewsDetail1_fvhotnewsdetail",
            'toaan.gov.vn' => "cl_newContent",
            'sggp.org.vn' => "cl_NewsConomicDetails",
            'petrotimes.vn' => "id_content",
            'anninhthudo.vn' => "cl_article",
            'tintuconline.com.vn' => "id_newsContent",
            'vtc.vn' => "cl_new_detail",
            'eva.vn' => "id_content_eva",
            'vietnamnet.vn' => "id_content",
            'autopro.com.vn' => "id_divTinChiTiet",
            'vneconomy.vn' => "cl_content",
            'thethaovanhoa.vn' => "id_divDetail",
            'baodatviet.vn' => "cl_detail_summary",
            'bongdaplus.vn' => "cl_article",
            'bongdaso.com' => "cl_art_content",
            'cafef.vn' => "id_mainDetail",
            'ione.net' => "cl_dtContent",
            'cand.com.vn' => "id_Table12"
        );
        $isDefine = false;
        foreach ($arrobject as $key => $value) {
            if ($website == $key) {
                if (substr($value, 0, 2) == "id") {
                    $value = str_replace("id_", "", $value);
                    $obj = $dom->getElementById($value);
                } else {
                    $xpath = new DOMXPath($dom);
                    $value = str_replace("cl_", "", $value);
                    $obj = $xpath->query("/html/body//*[contains(@class, '$value')]")->item(0);
                }

                if (count($obj) > 0) {
                    $images = $obj->getElementsByTagName('img');
                    $i = 0;
                    foreach ($images as $key => $image) {
                        $src = $image->getAttribute('src');
                        if (strpos($src, "http://") === false)
                            $src = "http://" . $website . $src;
                        list($width, $height, $type, $attr) = getimagesize(@$src);
                        if ($width >= 50 && $height >= 50) {
                            $arrImg[$i] = $src;
                            $i++;
                        }
                    }
                }
                $isDefine = true;
            }
        }
        if ($isDefine == false) {
            $body = $dom->getElementsByTagName('body')->item(0);
            $images = $body->getElementsByTagName('img');
            $i = 0;
            foreach ($images as $key => $image) {
                $src = $image->getAttribute('src');
                if (strpos($src, "http://") === false)
                    $src = "http://" . $website . $src;
                list($width, $height, $type, $attr) = getimagesize(@$src);
                if ($width >= 50 && $height >= 50) {
                    $arrImg[$i] = $src;
                    $i++;
                }
            }
        }
        return $arrImg;
    }

    function get_content($url) {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);

        ob_start();

        curl_exec($ch);
        curl_close($ch);
        $string = ob_get_contents();

        ob_end_clean();

        return $string;
    }

    function curl_file_get_content($URL) {
        $contents = '';
        $handle = fopen($URL, 'r');
        if ($handle) {
            while (!feof($handle)) {
                $contents .= fread($handle, 8192);
            }
        }
        return $contents;
    }

}

function file_get_contents_utf8($fn) {
    $opts = array(
        'http' => array(
            'method' => "GET",
            'header' => "Content-Type: text/html; charset=utf-8"
        )
    );

    $context = stream_context_create($opts);
    $result = @file_get_contents($fn, false, $context);
    return $result;
}

?>
